import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { loadEnv } from 'vite'
// nodejs写法，获取项目目录
import path from 'path';

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  return defineConfig({
    plugins: [vue()],
    server: {
      host: "127.0.0.1",
      port:Number(loadEnv(mode,process.cwd()).VITE_APP_PORT),
      strictPort: true,
      https: false,
      open: true,//在开放服务器启动时自动在浏览器中打开应用程序
      proxy: { //代理配置
        '/api': {
          target: loadEnv(mode,process.cwd()).VITE_APP_URL,
          changeOrigin: true, //跨域配置
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      },
      hmr: {
        overlay: false
      }
    },
    resolve: {//解析设置项目导入路径
      alias: {
        '@': path.resolve(__dirname, './src')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/styles/global.scss";'
        }
      }
    },
    build: { // 分块打包配置 
      chunkSizeWarningLimit: 1500,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_moudules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    }
  })
}

